package com.wontube.smr.bo.sites.youtube;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.wontube.client.extension.utils.ArrayUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.smr.bo.constants.SiteConstants;
import com.wontube.smr.bo.constants.StandarMovieGenres;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.dao.bo.GenreSelectDao;
import com.wontube.smr.web.client.model.po.GenreSite;

public final class YoutubeMovieGenres {
	private YoutubeMovieGenres(){}
	
	private static final Map<String, String> siteStandarMap = new HashMap<String, String>();		
	private static final Map<String, String> map = new HashMap<String, String>();		
	private static final List<Genre> i18nGenres = new ArrayList<Genre>();
	
	static {
		try {
			loadYoutubeMovieGenersMap();
			mapStandarGenres();
			initI18nGenres();
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	private static void loadYoutubeMovieGenersMap(){
		List<GenreSite> genreSites = GenreSelectDao.selectGenreSites(SiteConstants.WEBSITE_YOUTUBE, 
				Genre.CATE_MOVIE);
		for (GenreSite gs : genreSites) {
			siteStandarMap.put(gs.getSiteGenre(), gs.getStandarGenre());
		}
	}
	
	private static void mapStandarGenres(){
		map.put("Action & Adventure", StandarMovieGenres.ACTION);
		map.put("Foreign Film", StandarMovieGenres.INTERNATIONAL);
		map.put("Indian Cinema", StandarMovieGenres.INTERNATIONAL);
		map.put("Nigerian Cinema", StandarMovieGenres.INTERNATIONAL);
		map.put("Mystery & Suspense", StandarMovieGenres.HORROR);
		map.put("Science Fiction", StandarMovieGenres.SCIENCE);
		
		map.put("Adolescence", StandarMovieGenres.OTHERS);
	}
	
	private static void initI18nGenres() {
		i18nGenres.add(new Genre(StandarMovieGenres.ACTION, "Action and Adventure"));
		i18nGenres.add(new Genre(StandarMovieGenres.ANIMATION, "Animation"));
		i18nGenres.add(new Genre(StandarMovieGenres.CLASSICS, "Classics"));
		i18nGenres.add(new Genre(StandarMovieGenres.COMEDY, "Comedy"));
		i18nGenres.add(new Genre(StandarMovieGenres.CRIME, "Crime"));
		i18nGenres.add(new Genre(StandarMovieGenres.Documentary, "Documentary"));
		i18nGenres.add(new Genre(StandarMovieGenres.DRAMA, "Drama"));
		i18nGenres.add(new Genre(StandarMovieGenres.FAMILY, "Family"));
		i18nGenres.add(new Genre(StandarMovieGenres.HORROR, "Horror"));
		i18nGenres.add(new Genre(StandarMovieGenres.ROMANCE, "Romance"));
		i18nGenres.add(new Genre(StandarMovieGenres.SCIENCE, "Science Fiction"));
		i18nGenres.add(new Genre(StandarMovieGenres.OTHERS, "Others"));
	}	
	
	public static List<Genre> getI18nGenres(){
		return i18nGenres;
	}
	
	public static String[] getI18nOtherGenres(){
		//TODO
		return new String[]{StandarMovieGenres.OTHERS, StandarMovieGenres.SPORTS,  
				StandarMovieGenres.INTERNATIONAL, StandarMovieGenres.WAR};
	}
	
	
	/**
	 * 修正单个genre
	 * @param genreName
	 * @return
	 */
	public static String remendGenre(String genreName){
		if(StringUtils.isNullOrEmpty(genreName)){
			return null;
		}
		
		//处理专有genre, 不存在则判断是标准genre
		genreName = genreName.trim();
		String genre = map.get(genreName);
		return genre != null ? genre : genreName;		
	}
	
	/**
	 * 修正以逗号分隔的genres
	 * @param genreNames
	 * @return
	 */
	public static String remendAllGenres(String genreNames){
		if(StringUtils.isNullOrEmpty(genreNames)){
			return null;
		}
		
		Set<String> genreSet = new HashSet<String>();
		
		String[] genres = genreNames.split("\\s*,\\s*");
		for (String g : genres) {
			String rg = remendGenre(g);
			if(rg != null){
				genreSet.add(rg);
			}		
		}
		
		return ArrayUtils.join(genreSet, ",");
	}
}
